﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RestaurateurConsole
{
    public static class ConfigHandler
    {
        public readonly static Config Configuration = Config.GetConfig();
    }

    [Serializable]
    public class Config
    {
        private Stack<int> unusedSalesIDs = new Stack<int>();
        private int _SalesID = -1;
        private int _SoldItemsID = -1;
        public decimal TaxRate { get; set; }
        public string[] Address { get; set; }
        public string PhoneNumber { get; set; }
        public DateTime LastRunDate { get; set; }
        public string RegistrationKey { get; set; }
        public bool FirstRun { get; set; }
        public int NoOfTables { get; set; }
        public decimal TaxAdditionThreshhold { get; set; }
        public int SalesID
        {
            get
            {
                return _SalesID;
            }
            set
            {
                _SalesID = value;
            }
        }

        public Stack<int> UnusedSalesID
        {
            get
            {
                if (unusedSalesIDs == null)
                    unusedSalesIDs = new Stack<int>();
                return unusedSalesIDs;
            }
        }

        public int SoldItemsID
        {
            get
            {
                return _SoldItemsID;
            }
            set
            {
                _SoldItemsID = value;
            }
        }

        public int GetNewSalesID()
        {
            if (this.UnusedSalesID.Count == 0)
                return ++_SalesID;
            else
                return this.UnusedSalesID.Pop();
        }

        public int GetNewSoldItemsID()
        {
            return ++_SoldItemsID;
        }

        public static Config GetConfig()
        {
            SerializationHandler configHandler = new SerializationHandler(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Restaurateur", "RestaurateurMenu.config");
            try
            {
                return configHandler.DeSerialize<Config>();
            }
            catch
            {
                Config c = new Config() { TaxRate = (decimal)0, PhoneNumber = "9376179741" };
                c.Address = new string[] { "Shop No. 1, Shreyashrayam", "Sargasan Road, Behind DA-IICT", "Gandhinagar, Gujarat" };
                c.FirstRun = true;
                c.LastRunDate = DateTime.Now;
                c.RegistrationKey = "";
                c.NoOfTables = 21;
                c.TaxAdditionThreshhold = (decimal)0;
                c.Save();
                return c;
            }
        }

        public void Save()
        {
            SerializationHandler configHandler = new SerializationHandler(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Restaurateur", "RestaurateurMenu.config");
            configHandler.Serialize<Config>(this);
        }

    }
}
